package com.cloudcc.mobile.entity;

import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.util.AESUtils;
import com.cloudcc.mobile.util.Tools;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.NotNull;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;

@Table("login")
public class LoginDBModel {

	public static final String COL_USERNAME = "username";
	public static final String COL_LASTLOGIN = "lastlogin";
	public static final String COL_USERID = "userid";
	public static final String COL_ISMD5 = "ismd5";
	public static final String COL_IS_AUTO = "isAutoLogin";
	public static final String COL_IS_REMEMBER = "isRememberUser";

	@NotNull
	@PrimaryKey(AssignType.BY_MYSELF)
	@Column(COL_USERNAME)
	public String userName;// 用户名
	public String password; // 密码加密后的
	@NotNull
	@Column(COL_LASTLOGIN)
	public long lastLogin; // 最后登录的时间

	@Column(COL_USERID)
	public String userId;// userId

	@Column(COL_ISMD5)
	public boolean isMD5 = false;// 默认是 不md5

	@Column(COL_IS_AUTO)
	public boolean isAutoLogin = true;// 默认是 自动登录 记住密码

	@Column(COL_IS_REMEMBER)
	public boolean isRememberUser = true;// 默认是记住用户名

	/**
	 * 获得解密后的密码
	 * 
	 * @return
	 */
	public String getDecrypt() {
		String depassword = StringUtils.EMPTY;
		if (StringUtils.isNotBlank(password)) {
			try {
				depassword = AESUtils.decrypt(Tools.getpassword(), password);
				return depassword;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return password;
	}

	/**
	 * 将明文密码 des加密成 密文
	 * 
	 * @return 加密后的密文 2016-5-3
	 */
	public String getPasswordEncrypt() {
		String enPassword = StringUtils.EMPTY;
		try {
			enPassword = AESUtils.encrypt(Tools.getpassword(), password);
			return enPassword;
		} catch (Exception e) {
			e.printStackTrace();
			return enPassword;
		}
	}

	/**
	 * 公共方法 用于 加密密码
	 * 
	 * @param password
	 *            密文密码
	 * @return 2016-5-3
	 */
	public static String encryptPassword(String password) {
		String enPassword = StringUtils.EMPTY;
		try {
			enPassword = AESUtils.encrypt(Tools.getpassword(), password);
			return enPassword;
		} catch (Exception e) {
			e.printStackTrace();
			return enPassword;
		}
	}

}
